﻿using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;

public class MenuBuilder
{

    private static List<Menu> _menuList;
    public static string GenerateMenu()
    {

        StringBuilder Menu = new StringBuilder();
        Control webControl = new Control();
        string currentFolder = GetCurrentFolder();


        foreach (Menu section in GetListOfSections())
        {
            if (currentFolder.ToLower() == section.Folder.ToLower())
            {
                Menu.Append(string.Format("<li class=\"selected\"><a href=\"{0}\">{1}</a></li>", webControl.ResolveUrl(section.URL), section.Name));
            }
            else
            {
                Menu.Append(string.Format("<li><a href=\"{0}\">{1}</a></li>", webControl.ResolveUrl(section.URL), section.Name));
            }

        }

        return Menu.ToString();

    }

    public static string GenerateSubMenu()
    {
        StringBuilder Menu = new StringBuilder();
        Control webControl = new Control();
        string currentFolder = GetCurrentFolder();

        foreach (Menu section in GetListOfSections())
        {
            if (currentFolder.ToLower() == section.Folder.ToLower())
            {
                foreach (var subMenu in section.SubMenu)
                {
                    Menu.Append(string.Format("<li><a href=\"{0}\">{1}</a></li>", subMenu.URL, subMenu.Name));

                }
            }

        }

        return Menu.ToString();

    }

    public static List<Menu> GetListOfSections()
    {
        if (_menuList == null)
        {
            _menuList = new List<Menu>();

            Menu aMenu = default(Menu);
            aMenu = new Menu("Order", "Orders", "~/Order/OrderList.aspx");
            aMenu.SubMenu.Add(new SubMenu("Create Order", "CreateOrder.aspx"));
            aMenu.SubMenu.Add(new SubMenu("List Orders", "OrderList.aspx"));
            _menuList.Add(aMenu);

            aMenu = new Menu("Customer", "Customers", "~/Customer/CustomerList.aspx");
            aMenu.SubMenu.Add(new SubMenu("Add Customer", "CustomerDetail.aspx"));
            aMenu.SubMenu.Add(new SubMenu("List Customers", "CustomerList.aspx"));
            _menuList.Add(aMenu);

            aMenu = new Menu("Product", "Products", "~/Product/ProductList.aspx");
            aMenu.SubMenu.Add(new SubMenu("Add Product", "ProductDetail.aspx"));
            aMenu.SubMenu.Add(new SubMenu("List Products", "ProductList.aspx"));
            _menuList.Add(aMenu);

        }

        return _menuList;
    }

    public static string GetCurrentFolder()
    {

        string filePath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
        System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
        string currentDirectory = fileInfo.DirectoryName;
        string currentFolder = string.Empty;

        if (currentDirectory.Contains("\\"))
        {
            currentFolder = Strings.Right(currentDirectory, currentDirectory.Length -
                (currentDirectory.LastIndexOf("\\") + 1));
        }

        return currentFolder;
    }

}
